package com.example.mybiadushoujiweishi.activity.acivitys.services;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.BanbenUpdateActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.Myuitl;
import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;


import java.io.File;

import okhttp3.Call;
import okhttp3.Request;

public class MyService extends Service {
    public MyService() {

    }


    NotificationCompat.Builder notificationBuild;
    NotificationManager notificationManager;

    public void createNotification() {

        notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuild = new NotificationCompat.Builder(MyApplication.getContext()); //考虑向下兼容
        notificationBuild.setContentTitle("下载中……")
                .setContentText("准备下载……")
                .setSmallIcon(R.mipmap.logo_small)
                .setOngoing(true) //设置他为一个正在进行的通知
                .setAutoCancel(false);

        notificationManager.notify(0, notificationBuild.build());

    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String url = intent.getStringExtra("apkurl");
        // Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
        createNotification();
        File file = Myuitl.getDir();
        downloadfile(url, file);

        return super.onStartCommand(intent, flags, startId);
    }

    public void downloadfile(String url, File file) {


        System.out.println(url + "download}}}}}}}}}}}}}}}}}}}}}}}]");
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(file.getAbsolutePath(), "myshoujiweishi.apk") {


                    @Override
                    public void onError(Call call, Exception e, int id) {
                        notificationBuild.setContentTitle("下载");
                        notificationBuild.setContentText("下载失败");
                        notificationManager.notify(0, notificationBuild.build());

                    }

                    @Override
                    public void onResponse(File response, int id) {

                      notificationBuild.setContentTitle("下载完毕");

                       // notificationBuild.setOngoing(false);
                        notificationBuild.setAutoCancel(true);
                        notificationManager.notify(0, notificationBuild.build());



                        Intent intent = Myuitl.install(response);
                        KLog.e("aaaaa" + response);
                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyService.this);
                        stackBuilder.addParentStack(BanbenUpdateActivity.class);
                        stackBuilder.addNextIntent(intent);
                        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                        notificationBuild.setContentIntent(resultPendingIntent);
                        notificationBuild.setProgress(0, 0, false);
                        notificationBuild.setContentText("点击安装");
                        notificationManager.notify(0, notificationBuild.build());
                    }

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        notificationBuild.setProgress(100, (int) (progress * 100), false);
                        notificationBuild.setContentText("已经下载了" + (int) (progress * 100) + "%");
                        notificationManager.notify(id, notificationBuild.build());

                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                    }

                  /*  @Override
                    public void onResponse(File response, int id) {
                        //Log.e(TAG, "onResponse :" + response.getAbsolutePath());
                        KLog.e("aaaaa"+response);
                        notificationBuild.setContentText("点击安装");
                        notificationBuild.setContentTitle("下载完毕");
                        KLog.e("bbbb"+response);
                        notificationBuild.setProgress(0, 0, false);
                        notificationBuild.setOngoing(false);
                        notificationBuild.setAutoCancel(true);
                      *//*  notificationBuild.setContentTitle("下载完成");
                        notificationBuild.setContentText("下载完毕");*//*
                        // install(response);
                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyService.this);
                        stackBuilder.addParentStack(BanbenUpdateActivity.class);
                        Intent intent = Myuitl.install(response);

                        stackBuilder.addNextIntent(intent);
                        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                        notificationBuild.setContentIntent(resultPendingIntent);

                        notificationManager.notify(0, notificationBuild.build());
                    }*/


                });


    }


}
